package com.example.guideapp.activity;

import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.example.guideapp.R;
import com.example.guideapp.bean.MyFestival;
import com.example.guideapp.fragment.Fragment3;
import com.example.guideapp.utils.Constant;
import com.example.guideapp.utils.DatabaseManage;
import com.example.guideapp.utils.MySqliteDatabaseHelper;

import java.util.Calendar;

public class ShowMyFestivalActivity extends AppCompatActivity {

    private EditText et_show_name;
    private EditText et_show_date;
    private EditText et_show_remark;

    private Button btn_show_update;
    private Button btn_show_save;

    private boolean noOrYes = true;

    private MyFestival myFestival;

    private MySqliteDatabaseHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_my_festival);

        initView();

        btn_show_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (noOrYes){
                    isTrue();

                }else {
                    isFalse();
                }
            }
        });

        btn_show_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                save();
            }
        });
    }

    //保存
    private void save() {
        SQLiteDatabase db = helper.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Constant.MYFESTIVAL_NAME,et_show_name.getText().toString().trim());
        contentValues.put(Constant.MYFESTIVAL_DATE,et_show_date.getText().toString().trim());
        contentValues.put(Constant.MYFESTIVAL_REMARK,et_show_remark.getText().toString().trim());
        int a = db.update(Constant.TABLE_MYFESTIVAL_NAME,contentValues,Constant.MYFESTIVAL_ID+"=?",new String[]{myFestival.getId()+""});
        db.close();
        if (a == 0){
            Toast.makeText(ShowMyFestivalActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(ShowMyFestivalActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
        }

        btn_show_update.setText("修改");
        btn_show_save.setVisibility(View.GONE);

        et_show_name.setEnabled(false);
        et_show_date.setEnabled(false);
        et_show_remark.setEnabled(false);

        noOrYes = true;
    }

    //取消
    private void isFalse() {
        btn_show_update.setText("修改");
        btn_show_save.setVisibility(View.GONE);

        et_show_name.setEnabled(false);
        et_show_date.setEnabled(false);
        et_show_remark.setEnabled(false);

        et_show_name.setText(myFestival.getName());
        et_show_date.setText(myFestival.getDate());
        et_show_remark.setText(myFestival.getRemark());

        noOrYes = true;
    }

    //修改
    private void isTrue() {
        btn_show_save.setVisibility(View.VISIBLE);
        btn_show_update.setText("取消");

        et_show_name.setEnabled(true);
        et_show_date.setEnabled(true);
        et_show_remark.setEnabled(true);

        et_show_name.setSelection(et_show_name.getText().length());

        et_show_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickDlg();
            }
        });

        et_show_date.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    showDatePickDlg();
                    return true;
                }
                return false;
            }
        });
        noOrYes = false;
    }

    //初始化布局
    private void initView() {
        et_show_name = (EditText) findViewById(R.id.et_show_name);
        et_show_date = (EditText) findViewById(R.id.et_show_time);
        et_show_remark = (EditText) findViewById(R.id.et_show_remark);

        btn_show_update = (Button) findViewById(R.id.btn_show_update);
        btn_show_save = (Button) findViewById(R.id.btn_show_save);

        SpannableString spannableString = new SpannableString("空...");
        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(15,true);
        spannableString.setSpan(absoluteSizeSpan,0,spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        et_show_remark.setHint(new SpannedString(spannableString));

        helper = DatabaseManage.getInstance(this);
        myFestival = (MyFestival) getIntent().getSerializableExtra(Fragment3.LISTVIEW_ITEM);

        et_show_name.setText(myFestival.getName());
        et_show_date.setText(myFestival.getDate());
        et_show_remark.setText(myFestival.getRemark());
    }

    //选择日期
    private void showDatePickDlg() {
        Calendar c = Calendar.getInstance();
        DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                et_show_date.setText(year+"-"+month+"-"+dayOfMonth);
            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
        dpd.show();
    }


}
